// 1. 输入(组件类型，图表,表单，媒体，指标，表格，其它)
// 2. 创建新组件文件夹并创建组件配置项文件
// 3. 根据组件类型创建对应的新组件index.vue
// 4. 注册组件
// 5. 配置数据源功能
// 6. 图表类型的需要特殊配置

import fs, { exists } from 'fs';
import prettier from 'prettier';
import chalk from 'chalk';
import template from './template/index.js';
import question from './question/index.js';
import option from './template/option.js';

const answer = await question();

exists(getCliCompPath(), e => {
	if (e) {
		console.log(chalk.rgb(251, 197, 49)(`${answer.compName}组件已存在，请重新输入组件名`));
	} else {
		console.log(chalk.rgb(251, 197, 49)(`${answer.compName}组件创建中，请稍后...`));
		// 创建新组件文件夹
		fs.mkdirSync(getCliCompPath());
		console.log(chalk.rgb(251, 197, 49)(`${answer.compName}组件文件夹已创建`));

		// 获取对应组件类型的组件模板
		let code = template(answer);
		code = prettier.format(code, prettierConfig('vue'));

		// 图表类型组件------新增src/echart/config.js配置
		if (answer.compType == 'chart') {
			fs.readFile(`${getCompPath()}/config.js`, 'utf-8', (err, data) => {
				if (!err) {
					let content = null;
					let [front, behind] = data.split('],');
					content = front + `,'${answer.compName}'` + '\n' + '],' + '\n' + behind;
					content = prettier.format(content, prettierConfig('babel'));

					fs.writeFile(`${getCompPath()}/config.js`, content, err => {
						if (!err) {
							console.log(chalk.rgb(251, 197, 49)(`${answer.compName}图表组件特殊配置已更新`));
						}
					});
				}
			});
		}

		// 创建新组件index.vue
		fs.writeFileSync(`${getCliCompPath()}/index.vue`, code);
		console.log(chalk.rgb(251, 197, 49)(`${answer.compName}组件index.vue文件已创建`));

		// 创建新组件配置项文件
		let optionTemplate = option(answer);
		fs.writeFileSync(`${getCompOptionPath()}/${answer.compName}.vue`, optionTemplate);
		console.log(chalk.rgb(251, 197, 49)(`${answer.compName}组件配置项文件已创建`));

		// 注册组件
		fs.readFile(`${getCompPath()}/index.js`, 'utf-8', (err, data) => {
			if (!err) {
				let content = null;
				let [front, behind] = data.split('export default');
				let [behindF, behindB] = behind.split('}');
				content = front + `import ${answer.compName} from './packages/${answer.compName}';` + 'export default' + behindF + `,${answer.compName}` + '}' + behindB;
				content = prettier.format(content, prettierConfig('babel'));

				fs.writeFile(`${getCompPath()}/index.js`, content, err => {
					if (!err) {
						console.log(chalk.rgb(251, 197, 49)(`${answer.compName}组件注册成功`));
						// 配置数据源功能
						if (answer.isDataSource) {
							fs.readFile(`${getDataSourcePath()}/dataSource.js`, 'utf-8', (err, data) => {
								if (!err) {
									let content = null;
									let [front, behind] = data.split('].concat(website.componentsList.filter(ele => ele.data === true).map(ele => ele.name));');
									content = front + `,'${answer.compName}'` + '\n' + '].concat(website.componentsList.filter(ele => ele.data === true).map(ele => ele.name));' + '\n' + behind;
									content = prettier.format(content, prettierConfig('babel'));

									fs.writeFile(`${getDataSourcePath()}/dataSource.js`, content, err => {
										if (!err) {
											console.log(chalk.rgb(251, 197, 49)(`${answer.compName}组件数据源配置成功`));
											console.log(chalk.rgb(144, 238, 144)(`${answer.compName}组件快速构建成功`));
										}
									});
								}
							});
						} else {
							console.log(chalk.rgb(144, 238, 144)(`${answer.compName}组件快速构建成功`));
						}
					}
				});
			}
		});
	}
});

function getCompPath() {
	return '.././src/echart';
}

function getCliCompPath() {
	return `.././src/echart/packages/${answer.compName}`;
}

function getDataSourcePath() {
	return '.././src/option';
}

function getCompOptionPath() {
	return `.././src/option/components`;
}

function prettierConfig(type) {
	return {
		parser: type,
		printWidth: 177,
		tabWidth: 2,
		useTabs: true,
		semi: true,
		singleQuote: true,
		bracketSpacing: true,
		trailingComma: 'none',
		arrowParens: 'avoid'
	};
}
